package com.example.back.config;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

@Component
public class SessionInterceptor implements HandlerInterceptor  {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
   //     System.out.println("拦截loginUrl:"+request.getReader().readLine()+"  "+request.getParameter("id")+"  "+request.getQueryString()+"  "+ UUID.randomUUID().toString().replace("-", ""));
        System.out.println(request.getParameter("name"));
        /*if (!request.getParameter("name").equals("123")){

            String result = JSON.toJSONString(XjxccException.error(101, "你还没有登录哦！"));
            returnJson(response, result);
            return false;
            *//*throw new XjxccException("登录信息已失效，请重新登录", 602);
            JSON.toJSONString()*//*
        }*/
        return true;

    }

    private void returnJson(HttpServletResponse response, String result) throws Exception {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(result);

        } catch (IOException e) {
        } finally {
            if (writer != null) {
                writer.close();
            }
        }

    }



}
